<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Session;
use think\paginator;
class Admin extends controller{
	private $admin;
	public function _initialize(){
		header("Content-Type: text/html;charset=utf-8");
		if(!Session::has('info_admin')){
			$this->success('请先登陆！','login/index');
		}
	}
	public function index(){
		return $this->fetch();
	}
	public function impo(){
		return $this->fetch();
	}
	public function importWrite(){//向数据库写入数据
/*		if($key!="1234"){
			$this->error("口令错误");
		}*/
		$file=request()->file('excelfile');
		$info=$file->validate(['ext'=>'xls,xlsx','size'=>10240000])->move(ROOT_PATH.'public/uploads');
		if($info){
			$fileName=$info->getSaveName();
			$fileName=ROOT_PATH . 'public' . DS . 'uploads'."/". $fileName;	
			if($info->getExtension()=='xls'){
				$objReader = \PHPExcel_IOFactory::createReader('Excel5');
			}else{
				$objReader = \PHPExcel_IOFactory::createReader('Excel2007');
			}
			/*$objReader = \PHPExcel_IOFactory::createReader('Excel5');*///创建excel2003读取对象
			$objPHPExcel = $objReader->load($fileName,$encode='utf-8');	//excel文件对象	
			$sheet = $objPHPExcel->getSheet(0);//取第一张工作表，工作表对象
			$highestRow = $sheet->getHighestRow();//取得总行数
			$highestColumn = $sheet->getHighestColumn(); //取得总列数
/*			var_dump($sheet->getCell('B2')->getValue());//打印单元格的值*/
			$uploadInfo=array();
			$writeNum=0;
			$dateList=array();
			$key=0;
			for($i=2;$i<=$highestRow;$i++){
				if($writeNum>=3000){
					$key++;
					$writeNum=0;
				}
				$stuno=$sheet->getCell("B".$i)->getValue();
				$truename=$sheet->getCell("C".$i)->getValue();
				$idnum=$sheet->getCell("D".$i)->getValue();
				$starttime=$sheet->getCell("E".$i)->getValue();
				$status=$sheet->getCell("F".$i)->getValue();
				$college=$sheet->getCell("G".$i)->getValue();
				/*$uploadInfo[]=array('truename'=>$truename,'stuno'=>$stuno,'idnum'=>$idnum);*/
				$dataList[$key][]=array(
								'stuno'=>$stuno,
								'truename'=>$truename,
								'idnum'=>$idnum,
								'starttime'=>$starttime,
								'status'=>$status,
								'college'=>$college,
				);
				$writeNum++;
			}
			$nums=0;
			foreach($dataList as $key=>$value){
				if($num=Db::table('tb_stu')->insertAll($value)){//向数据库写入数据
					$nums+=$num;
				}else{
					echo "写入失败";
				}
			}
			echo "成功写入".$nums."条数据";
		}
	}
	
	public function info(){//
		$info=Db::table("tb_stu")->paginate(10);
		$nowpage=$info->getCurrentPage();
		$this->assign('nowpage',$nowpage);
		$this->assign('info',$info);
		return $this->fetch('info');
	}
	public function ajaxGetInfo(){
		$id=input("post.id");
		$info=Db::table("tb_stu")->where("id",$id)->find();
		$info=json($info);
		return $info;
	}
	
	public function down(){//数据导出
		$fileName="数据导出表";//下载文件的名称
		$PHPExcel=new \PHPExcel();//创建一个PHPExcel对象
		$PHPExcel->createSheet();//创建工作表
		$subObject=$PHPExcel->getSheet(0);//选择第一个工作表，返回工作表对象
		$subObject->setTitle('数据表');//设置工作表名称
		$subObject->getRowDimension("A")->setRowHeight(30);//设置行高
		$subObject->mergeCells("A1:D1");//合并单元格
		//填入标题行
		$subObject->setCellValue('A1','学生信息表');//设置某个单元格的值
		$subObject->setCellValue('A2','序号');//设置某个单元格的值
		$subObject->setCellValue('B2','姓名');
		$subObject->setCellValue('C2','身份证号');
		$subObject->setCellValue('D2','学号');
		$info=Db::table("tb_stu")->select();//所有学生的信息，二维数组表示
		$k=1;//序号
		$r=3;//行号
		foreach($info as $value){
			$subObject->setCellValue('A'.($r),$k);
			$subObject->setCellValue('B'.($r),$value['truename']);
			$subObject->setCellValue('C'.($r),$value['idnum']);
			$subObject->setCellValue('D'.($r),$value['stuno']);
			$k++;
			$r++;
		}
		
		$objWriter=\PHPExcel_IOFactory::createWriter($PHPExcel,'Excel2007');//创建Excel写入对象
		ob_end_clean();//消除缓存，否则导出的将是乱码		
		header("Pragma: public");		
		header("Expires: 0");		
		header("Cache-Control:must-revalidate, post-check=0, pre-check=0");		
		header("Content-Type:application/force-download");		
		header("Content-Type:application/vnd.ms-execl");		
		header("Content-Type:application/octet-stream");		
		header("Content-Type:application/download");		
		$ua = $_SERVER["HTTP_USER_AGENT"];		
		header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');		
		if (preg_match("/Firefox/", $ua)) {//如果是火狐浏览器
			header('Content-Disposition: attachment; filename*="utf8\'\'' . $fileName . '.xlsx"');
		}else{		
			$fileName = urlencode($fileName);		
			header("Content-Disposition: attachment; filename=".$fileName.".xlsx");		
			}		
	    header('Cache-Control: max-age=0');		
		$objWriter->save( 'php://output');
	}
	public function test(){

	}
	public function count(){//显示完成情况的统计信息
		$collegeArr=Db::table('tb_stu')->distinct(true)->column('college');
		// var_dump($collegeArr);
		$info=array();
		foreach($collegeArr as $key => $value){
/*			$info[$key]['college']=$value;
			$info[$key]['allnum']=$value;
			$info[$key]['overnum']=$value;
			$info[$key]['pe']=$value;*/
			$info[$key]['college']=$value;
			$info[$key]['allnum']=Db::table('tb_stu')->where('college',$value)->count();
			//$info[$key]['overnum']=Db::table('tb_stu')->where('college',$value)->where('relation1','neq',' null')->count();//relation1判断为字符串（第一种写法）
			// $info[$key]['overnum']=Db::table('tb_stu')->where("college='".$value."' and relation1<> 'null'")->count();
			$where['college']=$value;
			$where['relation1']=['<>',"null"];
			$info[$key]['overnum']=Db::table('tb_stu')->where($where)->count();
			$info[$key]['pe']=round(($info[$key]['overnum']/$info[$key]['allnum'])*100,1)."%";
		}
		 //var_dump($info);
		$this->assign('info',$info);
		return $this->fetch('count');
		

	}
}
?>